package com.hxx.day8;

import org.junit.Test;

import java.io.*;

/**
 * 缓冲流：
 *      字节缓冲流：BufferedInputStream、BufferedOutputStream
 *      字符缓冲流：BufferedReader、BufferedWriter
 */
public class TestDemo1 {
    /**
     * 字节缓冲流
     */
    @Test
    public void test1(){
        try(InputStream inputStream = new FileInputStream("d:/zmx/one.txt");
            OutputStream outputStream = new FileOutputStream("d:/zmx/three.txt");
            //字节缓冲流
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        ){
            int tmp = 0;
            while ((tmp=bufferedInputStream.read()) != -1){
                bufferedOutputStream.write(tmp);
            }
            //刷新缓冲区
            bufferedOutputStream.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 字符缓冲流
     */
    @Test
    public void test2(){
        try(Reader reader = new FileReader("d:/zmx/one.txt");
            Writer writer = new FileWriter("d:/zmx/four.txt");
            //字符缓冲流
            BufferedReader bufferedReader = new BufferedReader(reader);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
        ){
            String tmp = null;
            while ((tmp=bufferedReader.readLine()) != null){
                bufferedWriter.write(tmp);
                bufferedWriter.newLine();
            }
            //刷新缓冲区
            bufferedWriter.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 字节字符转换流：InputStreamReader、OutputStreamWriter
     */
     @Test
    public void test3(){
        try(InputStream inputStream = new FileInputStream("d:/zmx/one.txt");
            OutputStream outputStream = new FileOutputStream("d:/zmx/five.txt");
            //字节字符转换流
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        ){
            int tmp = 0;
            //一次读取一个字符
            while ((tmp=inputStreamReader.read()) != -1){
                outputStreamWriter.write(tmp);
            }
            //刷新缓冲区
            outputStreamWriter.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 使用字节字符转换流、字符缓冲流完成文件的复制（上传）
     */
    @Test
    public void test4()throws Exception{
        //字节--字节转字符--字符转缓冲
        BufferedReader reader = new BufferedReader(new InputStreamReader(
               new FileInputStream("d:/zmx/one.txt")));
       //字节--字节转字符--字符转缓冲
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
               new FileOutputStream("d:/zmx/six.txt")));
       //复制
        String tmp = null;
        while ((tmp=reader.readLine()) != null){
            writer.write(tmp);
            writer.newLine();
        }
        //刷新缓冲区
        writer.flush();
        //关闭流
        reader.close();
        writer.close();
    }
    /**
      打印流：PrintStream、PrintWriter
     */
     @Test
    public void test5()throws Exception{
        try(PrintStream printStream = new PrintStream("d:/zmx/seven.txt");
            PrintWriter printWriter = new PrintWriter("d:/zmx/eight.txt");
        ){
            printStream.println("hello world");
            printWriter.println("hello world");
        }
    }
    /**
      打印流：PrintStream、PrintWriter
      重定向
     */
     @Test
    public void test6()throws Exception{
        //将标准输出流重定向到文件
        System.setOut(new PrintStream("d:/zmx/nine.txt"));
        System.out.println("日志：沙发是");
    }
}





























